package cn.livingCloud.controller.core;

import cn.livingCloud.dto.ReplySetFormDto;
import cn.livingCloud.dto.ReplySetListDto;
import cn.livingCloud.service.ReplySetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.validation.Valid;

/**
 * Created by 唐旭 on 2016/6/1.
 */
@Controller
@RequestMapping("/admin/reply_set")
public class ReplySetController {

    @Autowired
    private ReplySetService service;

    @RequestMapping("/list.htm")
    public String list(ReplySetListDto listDto, Model model) {
        service.loadList(listDto);
        model.addAttribute("listDto", listDto);
        return "reply_set/list";
    }

    @RequestMapping("/delete.htm")
    public String delete(@RequestParam("uuid") String uuid, Model model) {
        service.delete(uuid);
        model.addAttribute("delete", "success");
        return "redirect:list.htm";
    }

    @RequestMapping("/edit.htm")
    public String editForm(String uuid, Model model) {
        ReplySetFormDto formDto = service.loadForm(uuid);
        model.addAttribute("formDto", formDto);
        return "reply_set/edit";
    }

    @RequestMapping(value = "/edit.htm", method = RequestMethod.POST)
    public String submitForm(@ModelAttribute("formDto") @Valid ReplySetFormDto formDto, BindingResult result, Model model) {
        if (result.hasErrors()) {
            return "reply_set/edit";
        }
        service.updateReplySet(formDto);
        model.addAttribute("edit", "success");
        return "redirect:list.htm";
    }

}
